package com.icss.mqsp.extend.login;

import com.icss.mqsp.builder.IBuilder;
import com.icss.mqsp.db.DBManager;
import com.icss.mqsp.util.DataUtil;
import com.icss.mqsp.util.RequestCondition;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LoginBuilder
  implements IBuilder
{
  private static Log log = LogFactory.getLog(LoginBuilder.class);
  
  public JSONObject exec(RequestCondition rc)
  {
    String op = (String)rc.getCondition("opid");
    String username = (String)rc.getCondition("username");
    String password = (String)rc.getCondition("password");
    String modeltype = (String)rc.getCondition("modeltype");
    String name = "";
    String firm = "";
    String dpt = "";
    String mdpass = DataUtil.setPwdJM(password);
    String phoneuuid = (String)rc.getCondition("uuid");
    String model = (String)rc.getCondition("model");
    log.info("uuid值=" + phoneuuid + "model值=" + model);
    log.info("op值=" + op);
    JSONObject json = new JSONObject();
    Map conditionMap = new HashMap();
    conditionMap.put("username", username);
    conditionMap.put("password", mdpass);
    conditionMap.put("phoneuuid", phoneuuid);
    conditionMap.put("model", model);
    conditionMap.put("modeltype", modeltype);
    conditionMap.put("time", Long.valueOf(System.currentTimeMillis()));
    if (op.equals("verson"))
    {
      log.info("获取版本信息");
      List<Map<String, String>> userList = DBManager.queryForList("getVersion", conditionMap);
      if (userList.size() > 0)
      {
        Map map = (Map)userList.get(0);
        String version = (String)map.get("VERSON");
        log.info(modeltype + "版本号：" + version);
        json.put("version", version);
      }
      else
      {
        log.info(modeltype + "版本信息不存在");
        json.put("version", "0");
      }
    }
    else if (op.equals("registe"))
    {
      log.info("用户注册");
      Map insertUserMap = new HashMap();
      insertUserMap.put("USERNAME", username);
      insertUserMap.put("PASSWORD", mdpass);
      insertUserMap.put("MODEL", model);
      insertUserMap.put("UUID", phoneuuid);
      insertUserMap.put("FIRM", "");
      insertUserMap.put("DEPARTMENT", "");
      insertUserMap.put("NAME", "");
      int ret = DBManager.insert("insertMqspUser", insertUserMap);
      if (ret == 1) {
        json.put("isValidUser", "3");
      } else {
        json.put("isValidUser", "2");
      }
    }
    else if (op.equals("validate"))
    {
      log.info("启用停用");
      int flag = DBManager.update("validateUser", conditionMap);
      log.info("启用停用状态=" + flag);
      if (flag == 1) {
        json.put("validateUser", "1");
      } else {
        json.put("validateUser", "0");
      }
    }
    else if (op.equals("userManager"))
    {
      log.info("获取注册用户信息");
      conditionMap.put("time", DataUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
      List<Map<String, String>> dataList = DBManager.queryForList("getUserManage", conditionMap);
      String dataJson = DataUtil.listToJson(dataList);
      json.put("value", dataJson);
    }
    else
    {
      log.info("用户登录");
      List<Map<String, String>> userList = DBManager.queryForList("checkLogin", conditionMap);
      if (userList.size() > 0)
      {
        Map map = (Map)userList.get(0);
        String userFlag = (String)map.get("FLAG");
        if (userFlag.equals("1"))
        {
          log.info("用户验证通过");
          String usern = (String)map.get("USERNAME");
          log.info("用户id" + usern);
          json.put("isValidUser", "1");
          json.put("username", usern);
        }
        else
        {
          log.info("用户验证通过");
          json.put("isValidUser", "4");
        }
      }
      else
      {
        log.info("用户验证未通过");
        json.put("isValidUser", "5");
      }
    }
    return json;
  }
}
